package com.itxiaowu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itxiaowu.constant.MessageConstant;
import com.itxiaowu.entity.PageResult;
import com.itxiaowu.entity.QueryPageBean;
import com.itxiaowu.entity.Result;
import com.itxiaowu.pojo.Permission;
import com.itxiaowu.service.PermissionService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/permission")
public class PermissionController {

    @Reference
    private PermissionService permissionService;

    //获取所有权限数据分页显示
    @RequestMapping("/findPage.do")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = permissionService.findPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize()
                , queryPageBean.getQueryString());
        return pageResult;
    }

    //添加权限
    @RequestMapping("/add.do")
    public Result add(@RequestBody Permission permission){
        try {
            permissionService.add(permission);
            return new Result(true,"权限添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"权限添加有误");
        }
    }

    //根据id回显数据
    @RequestMapping("/findFormDataById.do")
    public Result findFormDataById(Integer id){
        try {
            Permission permission=permissionService.findFormDataById(id);


            return new Result(true,"权限信息查询成功",permission);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"权限信息查询失败!!");
        }
    }
    //更改权限数据
    @RequestMapping("/deletepermissionitem.do")
    public  Result deletepermissionitem(Integer id){
        try {
            permissionService.deleteById(id);
            return new Result(true,"权限删除成功!!");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"权限删除失败!!");
        }
    }
    //修改权限
    @RequestMapping("/edit")
    public Result edit(@RequestBody Permission permission){
        try {
            permissionService.edit(permission);
            return new Result(true,"权限修改成功!!");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"权限修改成功!!");
        }
    }

 }
